package com.egd.imapp.contoller.activity.me;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.contoller.activity.login.LoginActivity;
import com.egd.imapp.model.Model;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.ui.base.EaseBaseActivity;
import com.hyphenate.easeui.widget.EaseTitleBar;

public class AboutMeSetActivity extends EaseBaseActivity implements EaseTitleBar.OnBackPressListener, View.OnClickListener{
    public AboutMeSetActivity mContext;

    private EaseTitleBar title_bar_me_set;
    private LinearLayout item_set_security;
    private LinearLayout item_set_common;
    private LinearLayout item_set_notification;
    private LinearLayout item_set_privacy;
    private LinearLayout item_set_help;

    private Button btn_logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_me_set);

        mContext = this;

        initView();

        initListener();
    }

    private void initView() {
        title_bar_me_set = findViewById(R.id.title_bar_me_set);
        item_set_security = findViewById(R.id.item_set_security);
        item_set_common = findViewById(R.id.item_set_common);
        item_set_notification = findViewById(R.id.item_set_notification);
        item_set_privacy = findViewById(R.id.item_set_privacy);
        item_set_help = findViewById(R.id.item_set_help);
        btn_logout = findViewById(R.id.btn_logout);

    }

    private void initListener() {
        title_bar_me_set.setOnBackPressListener(this);
        item_set_security.setOnClickListener(this);
        item_set_common.setOnClickListener(this);
        item_set_notification.setOnClickListener(this);
        item_set_privacy.setOnClickListener(this);
        item_set_help.setOnClickListener(this);
        btn_logout.setOnClickListener(this);
    }

    @Override
    public void onBackPress(View view) {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm!=null&&getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null){
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                super.onBackPressed();
            }else {
                super.onBackPressed();
            }
        }else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_set_security :// 账号与安全
                Toast.makeText(mContext, "服务完善中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_set_common :// 通用设置
                Toast.makeText(mContext, "服务完善中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_set_notification :// 新消息提醒
                Toast.makeText(mContext, "服务完善中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_set_privacy :// 隐私
                Toast.makeText(mContext, "服务完善中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.item_set_help :// 帮助
                Toast.makeText(mContext, "服务完善中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_logout: // 退出登录
//                Toast.makeText(mContext, "服务完善中...",Toast.LENGTH_SHORT).show();
                logout();
                break;
        }
    }

    private void logout() {
        final ProgressDialog pd = new ProgressDialog(mContext);
        String ms = "正在退出登录...";
        pd.setMessage(ms);
        pd.setCanceledOnTouchOutside(false);
        pd.show();

        Model.getInstance().getGlobalThreadPool().execute(()->{
            EMClient.getInstance().logout(true,new EMCallBack() {

                @Override
                public void onSuccess() {

                    // 关闭DBHelper
                    Model.getInstance().getDbManager().close();

                    runOnUiThread(new Runnable() {
                        public void run() {
                            pd.dismiss();
                            startActivity(new Intent(mContext, LoginActivity.class));
                            finish();
                        }
                    });
                }

                @Override
                public void onProgress(int progress, String status) {

                }

                @Override
                public void onError(int code, String message) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            pd.dismiss();
                            Toast.makeText(mContext, "服务异常，请稍后再试...", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        });

    }

}